package com.gin.no_gay_alliance.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.DisplayMetrics;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;

public class URLImageParser implements Html.ImageGetter {

    private final Context context;
    private final TextView tvContent;
    private final int actX;//实际的宽  放大缩小基于textview的宽度
    private final int actY;

    public URLImageParser(Context context, TextView tvContent) {
        this.context = context;
        this.tvContent = tvContent;
        //获取全屏大小
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        //我的textview有左右留边  margin
        actX = metrics.widthPixels;
        actY = metrics.heightPixels;
    }

    @NonNull
    private static Bitmap getBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        if (drawable instanceof GifDrawable) {
            return ((GifDrawable) drawable).getFirstFrame();
        }
        System.out.println("drawable = " + drawable.getClass());
        throw new RuntimeException("类型未处理");
    }

    @Override
    public Drawable getDrawable(String source) {
        final URLDrawable urlDrawable = new URLDrawable();
        // 动图
        Glide.with(context).load(source).into(new CustomViewTarget<TextView, Drawable>(tvContent) {
            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                int x = resource.getMinimumWidth();
                int y = resource.getMinimumHeight();
                Bitmap bitmap = getBitmap(resource);
                Matrix matrix = new Matrix();
                if (x > actX || y > actY) {
                    matrix.postScale((float) (actX * 1.00 / x), (float) (actX * 1.00 / x));
                }
                urlDrawable.bitmap = Bitmap.createBitmap(bitmap, 0, 0, x, y, matrix, true);
                urlDrawable.setBounds(0, 0, urlDrawable.bitmap.getWidth(), urlDrawable.bitmap.getHeight());
                tvContent.invalidate();
                tvContent.setText(tvContent.getText()); // 解决图文重叠
            }
        });
        return urlDrawable;
    }

    public class URLDrawable extends BitmapDrawable {
        protected Bitmap bitmap;

        @Override
        public void draw(Canvas canvas) {
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, 0, 0, getPaint());
            }
        }
    }
}